<?php
/**
 * Introduction: 首页
 * @author: 杨陈鹏
 * @date: 2016/4/7 23:00
 * @email: yangchenpeng@cdlinglu.com
 */

namespace App\Controller\Index;


use Think\Controller;

class IndexController extends Controller
{

    static public $setting_logic;

    public function __construct()
    {

        self::$setting_logic = D('Common/Setting','Logic');

    }
    /**
     * Introduction: 分销宝典
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    /**
     * Introduction: 首页数据
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */

    public function index()
    {

        $name           = $this->name();
        $slide          = $this->slide();
        $category       = $this->category();
        $hot_goods      = $this->hotGoods();
        $hot_special    = $this->hotSpecial();
        $hot_activity   = $this->hotActivity();

        $ad             = $this->ad();

        $data['name'] = $name['data']['name'] ? $name['data']['name'] : [] ;

        $data['slide'] = $slide['data'] ? $slide['data'] : [] ;

        $data['category'] = $category['data'] ? $category['data'] : [] ;

        $data['hot_goods'] = $hot_goods['data'] ? $hot_goods['data'] : [] ;

        $data['hot_special'] = $hot_special['data'] ? $hot_special['data'] : [] ;

        $data['hot_activity'] = $hot_activity['data'] ? $hot_activity['data'] : [] ;

        $data['ad'] = $ad ? $ad : [] ;

        outPut(['code' => 200, 'data' => $data]);


    }
    /**
     * Introduction: 生成二维码
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    public function qr()
    {

        $text = I('get.text');

        $size = I('get.size',4);

        $qr = new \Think\QR($text);

        header("content-type: image/png");
        echo $qr->image($size);

    }
    /**
     * Introduction: 获取商城名称
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    private function name()
    {


        return self::$setting_logic->get('market_name');


    }
    /**
     * Introduction: 获取轮播图
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    private function slide()
    {

        return self::$setting_logic->get('home_slide');

    }
    /**
     * Introduction: 获取分类
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    private function category()
    {

        return self::$setting_logic->get('home_category');

    }
    /**
     * Introduction: 获取热门商品
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    private function hotGoods()
    {

        $goods_logic = D('Common/Goods','Logic');

        $setting = self::$setting_logic->get('hot_goods');

        $setting = trim($setting['data']['good_ids'],',');


        //如果后台设置过,调用设置
        if($setting){

            $setting = explode(',',$setting);

            $ids = '';

            foreach($setting as $v){

                $ids = trim($v);

            }

            $ids = trim($ids,',');

            return $goods_logic::getData("id IN($ids)",1,'id,title,description,thumb',['column' => 'goods_salenum', 'type' => 'desc'],200);

        }

        //如果没有设置,默认按照销售数量
        return $goods_logic::getData('',1,'id,title,description,thumb',['column' => 'goods_salenum', 'type' => 'desc'],5);

    }
    /**
     * Introduction: 获取热门商品
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    private function hotActivity()
    {

        $activity_logic = D('Common/Activity','Logic');

        $setting = self::$setting_logic->get('hot_activity');

        $setting = trim($setting['data']['activity_ids'],',');
        //如果后台设置过,调用设置
        if($setting){

            $setting = explode(',',$setting);

            $ids = '';

            foreach($setting as $v){

                $ids = trim($v);

            }

            $ids = trim($ids,',');

            return $activity_logic::getData("id IN($ids)",'id,title,description,thumb','persons desc',1, $nums = 200);

        }

        //如果没有设置,默认按照销售数量
        return $activity_logic::getData('','id,title,description,thumb', 'persons desc', $page = 1, $nums = 5);

    }

    /**
     * Introduction: 获取首页广告
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    private function ad()
    {

        $data = self::$setting_logic->get('home_ad');

        return $data['data'];

    }

    /**
     * Introduction: 获取热门专题
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    private function hotSpecial()
    {

        $special_logic = D('Common/Special','Logic');

        $setting = self::$setting_logic->get('hot_activity');

        $setting = trim($setting['data']['special_ids'],',');
        //如果后台设置过,调用设置
        if($setting){

            $setting = explode(',',$setting);

            $ids = '';

            foreach($setting as $v){

                $ids = trim($v);

            }

            $ids = trim($ids,',');

            return $special_logic::getData("id IN($ids)",'id,title,description,thumb','',1,200);

        }

        //如果没有设置,默认按照销售数量
        return $special_logic::getData('','id,title,description,thumb','comment desc',1,5);

    }

    /**
     * Introduction: 分销宝典
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    public function distributionBook()
    {

        $setting = self::$setting_logic->get('distribution_book');

        outPut($setting);

    }

}